class PagesController < ApplicationController
  
  before_filter :authenticate
   
  def showlist
     @page = Page.find(:all)
      respond_to do |format|
        format.html # show.html.erb
        format.xml  { render :xml => @page }
    end
  end

  def newcat
    @page = Page.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @page }
    end
  end
  
  def newpage
    @page = Page.new
    @categories = Category.find(:all)

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @page }
    end
  end

  def edit
    @page = Page.find(params[:id])
  end
  
  def editcontent
    @page = Page.find(params[:id])
  end
  
  def editimages
    @page = Page.find(params[:id])
    @image = Image.new()
  end
  
  def editlarge
    @page = Page.find(params[:id])
    @bigimage = Bigimage.new()
  end
  
  def edittextimage
    @page = Page.find(params[:id])
    @textimage = Textimage.new()
  end
  
  def createpage
    @page = Page.new(params[:page])
    @page.categoryNameUrl = Page.cleanUrl(@page.categoryName)
    @page.pageNameUrl = Page.cleanUrl(@page.pageName)
    @page.hasFlash = 1
    @page.hasAutoTitle = 1
    @page.hasBreadcrumb = 0
    @page.HTMLContent = "New Page."

    respond_to do |format|
      if @page.save
        flash[:notice] = 'Page was successfully created.'
        format.html { redirect_to(@page.getUrl()) }
      else
        format.html { render :action => "new" }
      end
    end
  end

  def create
    @page = Page.new(params[:page])
    @page.categoryNameUrl = Page.cleanUrl(@page.categoryName)
    @page.pageName = @page.categoryName
    @page.pageNameUrl = "index"

    respond_to do |format|
      if @page.save
        flash[:notice] = 'Page was successfully created.'
        format.html { redirect_to(redirect_to(@page.getUrl())) }
      else
        format.html { render :action => "new" }
      end
    end
  end

  # PUT /pages/1
  # PUT /pages/1.xml
  def update
    @page = Page.find(params[:id])

    respond_to do |format|
      if @page.update_attributes(params[:page])
        flash[:notice] = 'Page was successfully updated.'
        format.html { redirect_to(@page.getUrl) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @page.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /pages/1
  # DELETE /pages/1.xml
  def destroy
    @page = Page.find(params[:id])
    
    if @page.pageNameUrl == "index"
      for page in Page.find(:all, :conditions => ['categoryNameUrl = ?', @page.categoryNameUrl ])
        page.destroy
      end
    end
    @page.destroy

    respond_to do |format|
      flash[:notice] = 'Page was successfully deleted.'
      format.html { redirect_to("/index.html") }
      format.xml  { head :ok }
    end
  end
 
end